package com.gxp.util.security;

import java.security.*;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class Des {
    private static final String PASSWORD_CRYPT_KEY = "kEHrjohnWHCWtfeLeeD2013q";
    /** �����㷨,���� DES,DESede,Blowfish. */
    private final static String ALGORITHM = "DES";
    public static void main(String[] args) throws Exception {
        String md5Password = "gxpadmin990";
        String str = Des.encrypt(md5Password);
        System.out.println("str: " + str);
        str = Des.decrypt(str);
        System.out.println("str: " + str);
        
        
        System.out.println("===================  " + Des.encryptUserPassord("admin","admin123"));
        String arch = System.getProperty("sun.arch.data.model");
        System.out.println(arch);
        
        System.out.println(org.apache.commons.codec.digest.DigestUtils.sha256Hex("gxpmenghuan8890"));
       
    }
    
    /**
     * ����ݽ���DES����.
     * @param data �����DES���ܵ����
     * @return ���ؾ���DES���ܺ�����
     * @throws Exception
     * Creation date: 2007-7-31 - ����12:06:24
     */
    public final static String decrypt(String data) throws Exception {
        return new String(decrypt(hex2byte(data.getBytes()),
                PASSWORD_CRYPT_KEY.getBytes()));
    }
    /**
     * ����DES���ܹ����ݽ��н���.
     * @param data DES�������
     * @return ���ؽ��ܺ�����
     * @throws Exception
     * Creation date: 2007-7-31 - ����12:07:54
     */
    public final static String encrypt(String data) throws Exception  {
        return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY
                .getBytes()));
    }
    
    /**
     * ��ָ����key����ݽ���DES����.
     * @param data ����ܵ����
     * @param key DES���ܵ�key
     * @return ����DES���ܺ�����
     * @throws Exception
     * Creation date: 2007-7-31 - ����12:09:03
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // DES�㷨Ҫ����һ�������ε������Դ
        SecureRandom sr = new SecureRandom();
        // ��ԭʼ�ܳ���ݴ���DESKeySpec����
        DESKeySpec dks = new DESKeySpec(key);
        // ����һ���ܳ׹�����Ȼ�������DESKeySpecת����
        // һ��SecretKey����
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher����ʵ����ɼ��ܲ���
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        // ���ܳ׳�ʼ��Cipher����
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        // ���ڣ���ȡ��ݲ�����
        // ��ʽִ�м��ܲ���
        return cipher.doFinal(data);
    }
    /**
     * ��ָ����key����ݽ���DES����.
     * @param data ����ܵ����
     * @param key DES���ܵ�key
     * @return ����DES���ܺ�����
     * @throws Exception
     * Creation date: 2007-7-31 - ����12:10:34
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // DES�㷨Ҫ����һ�������ε������Դ
        SecureRandom sr = new SecureRandom();
        // ��ԭʼ�ܳ���ݴ���һ��DESKeySpec����
        DESKeySpec dks = new DESKeySpec(key);
        // ����һ���ܳ׹�����Ȼ�������DESKeySpec����ת����
        // һ��SecretKey����
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher����ʵ����ɽ��ܲ���
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        // ���ܳ׳�ʼ��Cipher����
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        // ���ڣ���ȡ��ݲ�����
        // ��ʽִ�н��ܲ���
        return cipher.doFinal(data);
    }
    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("length is no even");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    }
    
    public static String encryptUserPassord(String username, String password) {
    	String saltpwd = password + "{" + username + "}";
    	return org.apache.commons.codec.digest.DigestUtils.sha256Hex(saltpwd);
    }

	public static String getRadomPwd(int pwd_len) {
		final int maxNum = 62;
		int i;
		int count = 0;
		char[] str = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
				'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
				'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
				'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
				'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6',
				'7', '8', '9', '0' };

		StringBuffer pwd = new StringBuffer("");
		Random r = new Random();
		while (count < pwd_len) {
			i = Math.abs(r.nextInt(maxNum));

			if (i >= 0 && i < str.length) {
				pwd.append(str[i]);
				count++;
			}
		}

		return pwd.toString();
	}
}